package com.project.decoration.controller.record;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.config.utils.ShiroUtils;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationRecord;
import com.project.decoration.domain.DecorationTask;
import com.project.decoration.domain.Result;
import com.project.decoration.domain.User;
import com.project.decoration.service.DecorationRecordService;
import com.project.decoration.service.DecorationTaskService;
import com.project.decoration.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 工作记录controller
 * @date 2025/02/17 01:47
 */
@Controller
@ResponseBody
@RequestMapping("record")
public class DecorationRecordController {

    @Autowired
    private DecorationRecordService decorationRecordService;
    @Autowired
    private DecorationTaskService decorationTaskService;
    @Autowired
    private UserService userService;

    /** 分页获取工作记录 */
    @PostMapping("getDecorationRecordPage")
    public Result getDecorationRecordPage(@RequestBody DecorationRecord decorationRecord) {
        Page<DecorationRecord> page = new Page<>(decorationRecord.getPageNumber(),decorationRecord.getPageSize());
        QueryWrapper<DecorationRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(decorationRecord.getTaskId()),DecorationRecord::getTaskId,decorationRecord.getTaskId())
                .like(StringUtils.isNotBlank(decorationRecord.getTitle()),DecorationRecord::getTitle,decorationRecord.getTitle())
                .like(StringUtils.isNotBlank(decorationRecord.getContent()),DecorationRecord::getContent,decorationRecord.getContent())
                .eq(StringUtils.isNotBlank(decorationRecord.getUserId()),DecorationRecord::getUserId,decorationRecord.getUserId());
        Page<DecorationRecord> decorationRecordPage = decorationRecordService.page(page, queryWrapper);
        return Result.success(decorationRecordPage);
    }

    @GetMapping("getDecorationRecordByProjectId")
    public Result getDecorationRecordByProjectId(@RequestParam("projectId")String projectId) {
        QueryWrapper<DecorationTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DecorationTask::getProjectId,projectId);
        List<DecorationTask> taskList = decorationTaskService.list(queryWrapper);
        List<DecorationRecord> recordList = new ArrayList<>();
        for (DecorationTask task : taskList) {
            QueryWrapper<DecorationRecord> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(DecorationRecord::getTaskId,task.getId());
            List<DecorationRecord> records = decorationRecordService.list(wrapper);
            for (DecorationRecord record : records) {
                User user = userService.getById(record.getUserId());
                record.setUserName(user.getUserName());
                record.setTel(user.getTel());
            }
            recordList.addAll(records);
        }
        return Result.success(recordList);
    }

    /** 根据id获取工作记录 */
    @GetMapping("getDecorationRecordById")
    public Result getDecorationRecordById(@RequestParam("id")String id) {
        DecorationRecord decorationRecord = decorationRecordService.getById(id);
        return Result.success(decorationRecord);
    }

    /** 保存工作记录 */
    @PostMapping("saveDecorationRecord")
    public Result saveDecorationRecord(@RequestBody DecorationRecord decorationRecord) {
        User userInfo = ShiroUtils.getUserInfo();
        decorationRecord.setUserId(userInfo.getId());
        boolean save = decorationRecordService.save(decorationRecord);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑工作记录 */
    @PostMapping("editDecorationRecord")
    public Result editDecorationRecord(@RequestBody DecorationRecord decorationRecord) {
        boolean save = decorationRecordService.updateById(decorationRecord);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除工作记录 */
    @GetMapping("removeDecorationRecord")
    public Result removeDecorationRecord(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationRecordService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("工作记录id不能为空！");
        }
    }

}
